#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <time.h>

#include <iostream>
#include <vector>
using namespace std;

#if (defined(UNICODE) || defined(_UNICODE))
#define _tcout std::wcout
#define _tcin  std::wcin
#else
#define _tcout std::cout
#define _tcin  std::cin
#endif

inline static int test_nodes_arg(_TCHAR *arg, std::size_t n_chars) {
	int result = 0;
	if (arg != NULL) {
		_TCHAR *parameter_id[] = { _T("nodes"), _T("n") };

		std::size_t param_id_len = 0;
		bool error_state = *arg != '-' || (_tcsncmp(parameter_id[0], arg + 1, param_id_len = std::min(_tcslen(parameter_id[0]), n_chars - 2)) != 0 
								&& _tcsncmp(parameter_id[1], arg + 1, param_id_len =  std::min(_tcslen(parameter_id[1]), n_chars - 2)) != 0) || *(arg + (++param_id_len)++) != _T(':');

		if (!error_state) {
			int result_len = _tcsclen(arg + param_id_len);
			if ( result_len > 0) result = _tstoi(arg + param_id_len);
		}
	}
	else _tcout << _T("Wrong parametr!");
	return result;
}

inline static int test_edges_arg(_TCHAR *arg, std::size_t n_chars) {
	int result = 0;
	if (arg != NULL) {
		_TCHAR *parameter_id[] = { _T("edges"), _T("e") };

		std::size_t param_id_len = 0;
		bool error_state = *arg != '-' || (_tcsncmp(parameter_id[0], arg + 1, param_id_len = std::min(_tcslen(parameter_id[0]), n_chars - 2)) != 0 
								&& _tcsncmp(parameter_id[1], arg + 1, param_id_len =  std::min(_tcslen(parameter_id[1]), n_chars - 2)) != 0) || *(arg + (++param_id_len)++) != _T(':');

		if (!error_state) {
			int result_len = _tcsclen(arg + param_id_len);
			if ( result_len > 0) result = _tstoi(arg + param_id_len);
		}
	}
	else _tcout << _T("Wrong parametr!");
	return result;
}

inline static int test_cost_arg(_TCHAR *arg, std::size_t n_chars) {
	int result = 0;
	if (arg != NULL) {
		_TCHAR *parameter_id[] = { _T("cost"), _T("c") };

		std::size_t param_id_len = 0;
		bool error_state = *arg != '-' || (_tcsncmp(parameter_id[0], arg + 1, param_id_len = std::min(_tcslen(parameter_id[0]), n_chars - 2)) != 0 
								&& _tcsncmp(parameter_id[1], arg + 1, param_id_len =  std::min(_tcslen(parameter_id[1]), n_chars - 2)) != 0) || *(arg + (++param_id_len)++) != _T(':');

		if (!error_state) {
			int result_len = _tcsclen(arg + param_id_len);
			if ( result_len > 0) result = _tstoi(arg + param_id_len);
		}
	}
	else _tcout << _T("Wrong parametr!");
	return result;
}

int _tmain(int argc, _TCHAR* argv[]) {
	int nodes = 0, edges = 0, max_cost = 0;
	for (size_t i = 1, max_i = static_cast<size_t>(argc); i < max_i; ++i) {
		size_t argv_str_len = _tcslen(argv[i]);
		for (std::size_t j = 0; j < argv_str_len; ++j) argv[i][j] = _totlower(argv[i][j]);

		if (!nodes) {
			nodes = test_nodes_arg(argv[i], argv_str_len);
			if (nodes) continue;
		}
		if (!edges) {
			edges = test_edges_arg(argv[i], argv_str_len);
			if (edges) continue;
		}
		if (!max_cost) {
			max_cost = test_cost_arg(argv[i], argv_str_len);
			if (max_cost) continue;
		}
	}

	if (!max_cost) max_cost = 10;

	vector < vector < int > > A(nodes);
	srand(time(0));
	while (edges > 0) {
		for (int i = 0; i < nodes; ++i) {
			A[i].resize(nodes);
			for (int j = i + 1; j < nodes && edges > 0; ++j) {
				if ((rand() % 10) < 5) continue;
				if (A[i][j] != 0) continue;
				edges--;
				A[i][j] = (rand() % max_cost);
			}
		}
	}

	for (int i = 0; i < nodes; ++i) {
		for (int j = 0; j < i; ++j) {
			A[i][j] = A[j][i];
		}
	}

	_tcout << nodes << std::endl;
	for (int i = 0; i < nodes; ++i) {
		for (int j = 0; j < nodes; ++j)
			_tcout << A[i][j] << " ";
		_tcout << std::endl;
	}

	return 0;
}